package com.sgq.ui.component;

import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.jface.operation.IRunnableWithProgress;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.wb.swt.SWTResourceManager;
import org.eclipse.swt.widgets.Label;

public class MainFrame {

	protected Shell shell;
	private MultSelectionCombo yearCombo;
	private Label lblNewLabel;

	public MainFrame() {
		open();
	}

	/**
	 * Open the window.
	 */
	public void open() {
		Display display = Display.getDefault();
		createContents();
		shell.open();
		shell.layout();
		while (!shell.isDisposed()) {
			if (!display.readAndDispatch()) {
				display.sleep();
			}
		}
	}

	/**
	 * Create contents of the window.
	 */
	protected void createContents() {
		shell = new Shell();
		shell.setImage(SWTResourceManager.getImage(MainFrame.class, "/images/logo.ico"));
		shell.setSize(800, 400);
		shell.setText("示例");
		
		Button button = new Button(shell, SWT.NONE);
		button.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				IRunnableWithProgress runnable = new IRunnableWithProgress() {
					@Override
					public void run(IProgressMonitor monitor) throws InvocationTargetException, InterruptedException {
						monitor.setTaskName("开始执行第一步");
						for (long i = 0; i < 30; i++) {
							System.out.println(i * i);
							Thread.sleep(100);
						}
						monitor.setTaskName("开始执行第二步");
						for (long i = 0; i < 30; i++) {
							System.out.println(i * i);
							Thread.sleep(100);
						}
						monitor.setTaskName("开始执行第三步");
						for (long i = 0; i < 30; i++) {
							System.out.println(i * i);
							Thread.sleep(100);
						}
					}
				};
				try {
					new LoadMaskDialog(shell).run(runnable );
				} catch (InvocationTargetException | InterruptedException e1) {
					e1.printStackTrace();
				}				
			}
		});
		button.setBounds(77, 10, 109, 27);
		button.setText("长时间耗时任务");
		
		yearCombo = new MultSelectionCombo(shell, getDataYear(), new int[]{}, new String[] {"2019"}, SWT.NONE);
		yearCombo.setBounds(77, 51, 147, 27);
		yearCombo.setText("2019");
		
		lblNewLabel = new Label(shell, SWT.NONE);
		lblNewLabel.setBounds(10, 56, 61, 17);
		lblNewLabel.setText("下拉选择框");
		
		Label label = new Label(shell, SWT.NONE);
		label.setBounds(10, 15, 61, 17);
		label.setText("遮罩层");

	}
	
	/**
	 * 获取下拉框的年度数据从当前年向后1年，向前10年
	 * @return
	 */
	private String[] getDataYear() {
		List<String> list = new ArrayList<>();
		int year = Calendar.getInstance().get(Calendar.YEAR);
		list.add(year + 1 + "");
		list.add(year + "");
	 	int count = 10;
	 	for (int i = 1; i <= count; i++) {
			list.add(year - i + "");
		}
		return list.toArray(new String[] {});
	}
	
	public static void main(String[] args) {
		new MainFrame();
	}
}
